package com.zzptc.easyshop.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

public class MysqlDB implements IDB{

    private  Connection conn;
    private Map map;
//    private  String  Driver="com.mysql.jdbc.Driver";
//    private  String url="jdbc:mysql://localhost:3306/easy_shop?useUnicode=true&characterEncoding=utf-8";
//    private  String user="root";
//    private  String password="root";
    private String Driver;
    private  String url;
    private  String user;
    private  String password;
    @Override
    public Connection getConnnection() throws SQLException {

        try {
            map=Myutil.readXML();
            Driver=(String) map.get("driver");
            url=(String) map.get("url");
            user=(String) map.get("username");
            password=(String) map.get("password");
        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            Class.forName(Driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        conn=DriverManager.getConnection(url,user,password);
        return conn;
    }

    @Override
    public void closeConn() throws SQLException {
        if(conn!=null&&!conn.isClosed()){
            conn.close();
        }
    }

    public static void main(String[] args) {
        IDB db=new MysqlDB();
        try {
            System.out.println(db.getConnnection().isClosed());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
